<?php

namespace app\admin\service;

use app\admin\model\BoxGame;
use app\admin\model\BoxGameLog;
use app\admin\model\BoxStatisticsAdOverviewLog;
use think\Db;

class BoxStatisticsGameOverviewService extends BaseService
{

    /**
     * @Notes:数据总览
     * @Interface getList
     * @return array
     */
    public function getList()
    {
        $param = request()->param();

        $map = [];

        if (!empty($param['time_range'])) {
            $param['time_range'][0] = implode('', explode('-', $param['time_range'][0]));
            $param['time_range'][1] = implode('', explode('-', $param['time_range'][1]));
            $map[]                  = ['day_time', '>=', $param['time_range'][0]];
            $map[]                  = ['day_time', '<=', $param['time_range'][1]];
        }
        if (isset($param['box_game_id']) && !empty($param['box_game_id'])) {
            $map[] = ['box_game_id', '=', $param['box_game_id']];
        }

        $list = (new BoxGameLog())
            ->field('day_time,sum(pay_amount) as pay_amount,box_game_id,sum(account_num) as account_num,sum(role_num) as role_num,sum(role_num_all) as role_num_all,sum(active_account_num) as active_account_num,sum(pay_account_num) as pay_account_num,sum(new_account_num) as new_account_num,sum(new_account_amount) as new_account_amount')
            ->where($map)
            ->order('day_time', 'desc');

        if (empty($param['box_game_id'])) {
            $list->group('day_time');
        } else {
            $list->group('day_time,box_game_id');
        }

        $list = $list->select()->toArray();
        //获取数据总数
        $count = count($list);

        $gameData = BoxGame::field(['id', 'game_name'])->select()->toArray();
        $gameData = array_column($gameData, 'game_name', 'id');

        $result = array();
        if (!empty($list)) {

            $pay_amount_all          = 0;
            $account_num_all         = 0;
            $role_num_all            = 0;
            $role_rate_all           = 0;
            $role_num_all_all        = 0;
            $active_account_num_all  = 0;
            $pay_account_num_all     = 0;
            $active_account_rate_all = 0;
            $arppu_all               = 0;
            $dappu_all               = 0;
            $new_account_num_all     = 0;
            $new_account_amount_all  = 0;
            $new_account_rate_all    = 0;
            $new_account_arppu_all   = 0;

            foreach ($list as $key => $item) {

                $result[$key]['day_time']            = date("Y-m-d", strtotime($item['day_time']));
                $result[$key]['pay_amount']          = $item['pay_amount'];
                $result[$key]['game_name']           = empty($param['box_game_id']) ? '全部游戏' : $gameData[$item['box_game_id']] ?? '';
                $result[$key]['account_num']         = $item['account_num'];
                $result[$key]['role_num']            = $item['role_num'];
                $result[$key]['role_rate']           = empty($item['account_num']) ? '0' : round($item['role_num'] / $item['account_num'] * 100, 2) . "%";
                $result[$key]['role_num_all']        = $item['role_num_all'];
                $result[$key]['active_account_num']  = $item['active_account_num'];
                $result[$key]['pay_account_num']     = $item['pay_account_num'];
                $result[$key]['active_account_rate'] = empty($item['active_account_num']) ? '0' : round($item['pay_account_num'] / $item['active_account_num'] * 100, 2) . "%";
                $result[$key]['arppu']               = empty($item['pay_account_num']) ? '0' : round($item['pay_amount'] / $item['pay_account_num'], 2);
                $result[$key]['dappu']               = empty($item['active_account_num']) ? '0' : round($item['pay_amount'] / $item['active_account_num'], 2);
                $result[$key]['new_account_num']     = $item['new_account_num'];
                $result[$key]['new_account_amount']  = $item['new_account_amount'];
                $result[$key]['new_account_rate']    = empty($item['account_num']) ? '0' : round($item['new_account_num'] / $item['account_num'] * 100, 2) . "%";
                $result[$key]['new_account_arppu']   = empty($item['new_account_num']) ? '0' : round($item['new_account_amount'] / $item['new_account_num'], 2);

                $pay_amount_all          += $item['pay_amount'];
                $account_num_all         += $item['account_num'];
                $role_num_all            += $item['role_num'];
                $role_rate_all           += empty($item['account_num']) ? '0' : round($item['role_num'] / $item['account_num'] * 100, 2);
                $role_num_all_all        += $item['role_num_all'];
                $active_account_num_all  += $item['active_account_num'];
                $pay_account_num_all     += $item['pay_account_num'];
                $active_account_rate_all += empty($item['active_account_num']) ? '0' : round($item['pay_account_num'] / $item['active_account_num'] * 100, 2);
                $arppu_all               += empty($item['pay_account_num']) ? '0' : round($item['pay_amount'] / $item['pay_account_num'], 2);
                $dappu_all               += empty($item['active_account_num']) ? '0' : round($item['pay_amount'] / $item['active_account_num'], 2);
                $new_account_num_all     += $item['new_account_num'];
                $new_account_amount_all  += $item['new_account_amount'];
                $new_account_rate_all    += empty($item['account_num']) ? '0' : round($item['new_account_num'] / $item['account_num'] * 100, 2);
                $new_account_arppu_all   += empty($item['new_account_num']) ? '0' : round($item['new_account_amount'] / $item['new_account_num'], 2);
            }
            $result[$count]['day_time']            = "汇总";
            $result[$count]['pay_amount']          = $pay_amount_all;
            $result[$count]['game_name']           = "-";
            $result[$count]['account_num']         = $account_num_all;
            $result[$count]['role_num']            = $role_num_all;
            $result[$count]['role_rate']           = round($role_rate_all / $count, 2) . "%";
            $result[$count]['role_num_all']        = $role_num_all_all;
            $result[$count]['active_account_num']  = $active_account_num_all;
            $result[$count]['pay_account_num']     = $pay_account_num_all;
            $result[$count]['active_account_rate'] = round($active_account_rate_all / $count, 2) . "%";
            $result[$count]['arppu']               = round($arppu_all / $count, 2);
            $result[$count]['dappu']               = round($dappu_all / $count, 2);
            $result[$count]['new_account_num']     = $new_account_num_all;
            $result[$count]['new_account_amount']  = $new_account_amount_all;
            $result[$count]['new_account_rate']    = round($new_account_rate_all / $count, 2) . "%";
            $result[$count]['new_account_arppu']   = round($new_account_arppu_all / $count, 2);
        }
        //返回结果
        return [
            "msg"   => '操作成功',
            "code"  => 0,
            "data"  => $result,
            "count" => $count,
        ];
    }

    /**
     * @Notes:数据总表
     * @Interface getList
     * @return array
     */
    public function gameTotalList()
    {
        $param = request()->param();

        $map = [];

        if (!empty($param['time_range'])) {
            $param['time_range'][0] = implode('', explode('-', $param['time_range'][0]));
            $param['time_range'][1] = implode('', explode('-', $param['time_range'][1]));
            $map[]                  = ['day_time', '>=', $param['time_range'][0]];
            $map[]                  = ['day_time', '<=', $param['time_range'][1]];
        }

        $list = (new BoxGameLog())
            ->field('box_game_id,sum(pay_amount) as pay_amount,sum(account_num) as account_num')
            ->where($map)
            ->order('box_game_id', 'desc')
            ->group('box_game_id');

        $list = $list->select()->toArray();

        $_gameLoginWhere   = [];
        $_gameLoginWhere[] = ['is_box', '=', 1];
        $_gameLoginWhere[] = ['add_time', 'between', [strtotime($param['time_range'][0]), strtotime($param['time_range'][1] . ' 23:59:59')]];
        //活跃账号数
        $active_account_nums = Db::connect('Db_log')->name('gamelogin_log')->where($_gameLoginWhere)->group('package_id')
            ->column('count(DISTINCT member_id) as active_account_num', 'package_id');

        $_orderWhere   = [];
        $_orderWhere[] = ['is_box', '=', 1];
        $_orderWhere[] = ['pay_time', 'between', [strtotime($param['time_range'][0]), strtotime($param['time_range'][1] . ' 23:59:59')]];
        //付费账号数
        $pay_account_nums = Db::connect('Db_log')->name('order_log')->where($_orderWhere)->group('package_id')
            ->column('count(DISTINCT member_id) as pay_account_num', 'package_id');

        //获取数据总数
        $count = count($list);

        $gameData = BoxGame::field(['id', 'game_name', 'package_id', 'ios_package_id', 'pc_package_id'])->select()->toArray();
        $gameData = array_column($gameData, null, 'id');

        $result = array();
        if (!empty($list)) {

            $pay_amount_all         = 0;
            $account_num_all        = 0;
            $active_account_num_all = 0;
            $pay_account_num_all    = 0;
            foreach ($list as $key => $item) {

                $result[$key]['game_name']          = $gameData[$item['box_game_id']]['game_name'] ?? '';
                $result[$key]['pay_amount']         = $item['pay_amount'];
                $result[$key]['account_num']        = $item['account_num'];
                $result[$key]['active_account_num'] = $this->iosAndroid($active_account_nums, $gameData[$item['box_game_id']]); //活跃账号数
                $result[$key]['pay_account_num']    = $this->iosAndroid($pay_account_nums, $gameData[$item['box_game_id']]);      //付费账号数

                $pay_amount_all         += $item['pay_amount'];
                $account_num_all        += $item['account_num'];
                $active_account_num_all += $this->iosAndroid($active_account_nums, $gameData[$item['box_game_id']]); //活跃账号数
                $pay_account_num_all    += $this->iosAndroid($pay_account_nums, $gameData[$item['box_game_id']]);      //付费账号数
            }
            $result[$count]['game_name']          = "汇总";
            $result[$count]['pay_amount']         = $pay_amount_all;
            $result[$count]['account_num']        = $account_num_all;
            $result[$count]['active_account_num'] = $active_account_num_all;
            $result[$count]['pay_account_num']    = $pay_account_num_all;
        }
        //返回结果
        return [
            "msg"   => '操作成功',
            "code"  => 0,
            "data"  => $result,
            "count" => $count,
        ];
    }

    public static function iosAndroid($value, $game)
    {
        return ($value[$game['package_id']] ?? 0) + ($value[$game['ios_package_id']] ?? 0) + ($value[$game['pc_package_id']] ?? 0);
    }

}
